
#include "stdafx.h"
#include "../client/DCPlusPlus.h"
#include "Resource.h"

#include "SlotPage.h"
//[-]PPA [Doxygen 1.5.1] #include "WinUtil.h"
#include "LineDlg.h"

#include "../client/Util.h"
#include "../client/ShareManager.h"
//[-]PPA [Doxygen 1.5.1] #include "../client/SettingsManager.h"
#include "../client/version.h"

PropPage::TextItem SlotPage::texts[] =
{
	{ IDC_SLOT_CONTROL_GROUP, ResourceManager::SLOT_CONTROL_GROUP },                    //[+] Drakon
	{ IDC_MINISLOT_CONTROL_GROUP, ResourceManager::MINISLOT_CONTROL_GROUP },            //[+] Drakon
	{ IDC_SETTINGS_SHARE_SIZE, ResourceManager::SETTINGS_SHARE_SIZE },
	{ IDC_SETTINGS_UPLOADS_MIN_SPEED, ResourceManager::SETTINGS_UPLOADS_MIN_SPEED },
	{ IDC_SETTINGS_KBPS, ResourceManager::KBPS },
	{ IDC_SETTINGS_KB, ResourceManager::KB },
	{ IDC_SETTINGS_UPLOADS_SLOTS, ResourceManager::SETTINGS_UPLOADS_SLOTS },
	{ IDC_SETTINGS_ONLY_HASHED, ResourceManager::SETTINGS_ONLY_HASHED },
	{ IDC_CZDC_SMALL_SLOTS, ResourceManager::SETCZDC_SMALL_UP_SLOTS },
	{ IDC_CZDC_SMALL_SIZE, ResourceManager::SETCZDC_SMALL_FILES },
	{ IDC_CZDC_NOTE_SMALL, ResourceManager::SETCZDC_NOTE_SMALL_UP },
	{ IDC_STATICb, ResourceManager::EXTRA_HUB_SLOTS },
	{ IDC_SLOT_DL, ResourceManager::EXTRASLOT_TO_DL }, // !SMT!-S
	{ IDC_SETTINGS_AUTO_SLOTS, ResourceManager::SETTINGS_AUTO_SLOTS },
	{ IDC_SETTINGS_PARTIAL_SLOTS, ResourceManager::SETCZDC_PARTIAL_SLOTS },
	{ IDC_GRANT_IP_GROUP, ResourceManager::GRANT_IP_GROUP },
	{ IDC_EXTRA_SLOT_BY_IP, ResourceManager::EXTRA_SLOT_BY_IP}, 
	{ IDC_GRANT_SLOT_FOR_MY_NET, ResourceManager::GRANT_SLOT_FOR_MY_NET},
	{ IDC_GRANTIP_INI_STAIC, ResourceManager::GRANTIP_INI_STAIC},
	{ 0, ResourceManager::SETTINGS_AUTO_AWAY }
};

PropPage::Item SlotPage::items[] =
{
	{ IDC_SLOTS, SettingsManager::SLOTS, PropPage::T_INT },
	{ IDC_MIN_UPLOAD_SPEED, SettingsManager::MIN_UPLOAD_SPEED, PropPage::T_INT },
	{ IDC_EXTRA_SLOTS, SettingsManager::EXTRA_SLOTS, PropPage::T_INT },
	{ IDC_SMALL_FILE_SIZE, SettingsManager::SET_MINISLOT_SIZE, PropPage::T_INT },
	{ IDC_EXTRA_SLOTS2, SettingsManager::HUB_SLOTS, PropPage::T_INT },
	{ IDC_SLOT_DL, SettingsManager::EXTRASLOT_TO_DL, PropPage::T_BOOL }, // !SMT!-S
	{ IDC_AUTO_SLOTS, SettingsManager::AUTO_SLOTS, PropPage::T_INT  },
	{ IDC_PARTIAL_SLOTS, SettingsManager::EXTRA_PARTIAL_SLOTS, PropPage::T_INT  },
	{ IDC_EXTRA_SLOT_BY_IP, SettingsManager::EXTRA_SLOT_BY_IP, PropPage::T_BOOL },
	{ IDC_GRANT_SLOT_FOR_MY_NET, SettingsManager::EXTRA_SLOT_FOR_MY_NET, PropPage::T_BOOL },
	{ 0, 0, PropPage::T_END }
};

LRESULT SlotPage::onInitDialog(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
	PropPage::translate((HWND)(*this), texts);
	
	PropPage::read((HWND)*this, items);
	
	CUpDownCtrl updown;
	updown.Attach(GetDlgItem(IDC_SLOTSPIN));
	updown.SetRange(1, 500); //[+]PPA
	updown.Detach();
	updown.Attach(GetDlgItem(IDC_MIN_UPLOAD_SPIN));
	updown.SetRange32(0, UD_MAXVAL);
	updown.Detach();
	updown.Attach(GetDlgItem(IDC_EXTRA_SLOTS_SPIN));
	updown.SetRange(3, 100);
	updown.Detach();
	updown.Attach(GetDlgItem(IDC_SMALL_FILE_SIZE_SPIN));
	updown.SetRange32(16, 32768);
	updown.Detach();
	updown.Attach(GetDlgItem(IDC_EXTRASPIN));
	updown.SetRange(0, 10);
	updown.Detach();
	updown.Attach(GetDlgItem(IDC_AUTO_SLOTS_SPIN));
	updown.SetRange(0, 100);
	updown.Detach();
	updown.Attach(GetDlgItem(IDC_PARTIAL_SLOTS_SPIN));
	updown.SetRange(0, 10);
	updown.Detach();
	

	try
	{
		m_IPGrantPATH = Util::getConfigPath() + "IPGrant.ini";
		string data = File(m_IPGrantPATH, File::READ, File::OPEN).read();
		m_IPGrant = data;
		SetDlgItemText(IDC_GRANTIP_INI, Text::toT(data).c_str());
		// SetDlgItemText(IDC_VIEWLINK_PATH, Text::toT(m_IPGrantPATH).c_str());
	}
	
	catch (const FileException&)
	{
		SetDlgItemText(IDC_GRANTIP_INI, Util::emptyStringT.c_str());
		// SetDlgItemText(IDC_VIEWLINK_PATH, CTSTRING(ERR_IPFILTER));
	}
	
	fixControls();


	return TRUE;
}

void SlotPage::write()
{
	PropPage::write((HWND)*this, items);

	AutoArray<TCHAR> l_buf(0xFFFFF);
	if (GetDlgItemText(IDC_GRANTIP_INI, l_buf.get(), 0xFFFFF))
	{
		string l_new = Text::fromT(l_buf.get());
		if (l_new != m_IPGrant)
		{
			try
			{
				File fout(m_IPGrantPATH, File::WRITE, File::CREATE | File::TRUNCATE);
				fout.write(l_new);
				fout.close();
				// PGLoader::getInstance()->LoadIPFilters();
			}
			catch (const FileException&)
			{
				return;
			}
		}
	}

}

void SlotPage::fixControls()
{
	bool state = (IsDlgButtonChecked(IDC_EXTRA_SLOT_BY_IP) != 0);
	::EnableWindow(GetDlgItem(IDC_GRANT_SLOT_FOR_MY_NET), state);
	::EnableWindow(GetDlgItem(IDC_GRANTIP_INI_STAIC), state);
	::EnableWindow(GetDlgItem(IDC_GRANTIP_INI), state);
}
